package com.example.tetris.slice;

import com.example.tetris.ResourceTable;
import com.example.tetris.beans.MyCommondialog;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;

public class QuestionSlice extends AbilitySlice implements TabList.TabSelectedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_question);
        initTabList();

        //获取装载内容的布局
        DirectionalLayout ct = (DirectionalLayout) findComponentById(ResourceTable.Id_ques_ct);
        DirectionalLayout game_th = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_game_teaching, null, false);
        //装载布局
        ct.addComponent(game_th);






        //返回页面
        Button bt_back = (Button) findComponentById(ResourceTable.Id_question_back);
        if (bt_back != null) {
            bt_back.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    terminateAbility();
                }
            });
        }



    }


    //初始化TabList
    private void initTabList() {
        TabList tablist = (TabList) findComponentById(ResourceTable.Id_recording_tablist);
        String[] tabTexts = {"玩法介绍", "开发背景"};
        for (int i = 0; i < tabTexts.length; i++) {
            TabList.Tab tab = tablist.new Tab(this);
            tab.setText(tabTexts[i]);
            tablist.addTab(tab);
            if (i == 0) {
                tab.select();
            }
        }
        //tablist事件监听
        tablist.addTabSelectedListener(this);

    }


    //tablist事件监听函数
    @Override
    public void onSelected(TabList.Tab tab) {
        int position = tab.getPosition();//索引
        //获取装载内容的布局
        DirectionalLayout ct = (DirectionalLayout) findComponentById(ResourceTable.Id_ques_ct);

        //每次点击先清空原有组件
        ct.removeAllComponents();

        if (position == 0) {
            DirectionalLayout game_th = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_game_teaching, null, false);
            //装载布局
            ct.addComponent(game_th);

        } else if (position == 1) {
            //获取开发背景的布局
            //通过LayoutScatter类对象的parse()方法将Layout_question_bg.xml形成一个Component 对象，方便后续的步骤使用。
            DirectionalLayout ques_bg = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_question_bg, null, false);
            //装载布局
            ct.addComponent(ques_bg);

            //语言切换
            Button bt_language = (Button) findComponentById(ResourceTable.Id_language);
            if (bt_language != null) {
                bt_language.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        DirectionalLayout dl = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                                .parse(ResourceTable.Layout_cmddialog_question, null, false);

                        MyCommondialog commondialog = new MyCommondialog(getContext(), dl);
                        //获取单选框对象,因为单选框是在“Layout_cmddialog_question”布局中，所以要先获得该布局对象，再在该布局中获得单选框对象
                        RadioContainer rc = (RadioContainer) dl.findComponentById(ResourceTable.Id_rc);
                        RadioButton zh = (RadioButton) dl.findComponentById(ResourceTable.Id_zh);
                        RadioButton en = (RadioButton) dl.findComponentById(ResourceTable.Id_en);
                        //当单选框中按钮状态发生改变时，触发下面监听事件
                        rc.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
                            @Override
                            //参数一：单选框按钮对象
                            //参数二：索引，表示当前改变的是按钮组中第几个按钮
                            public void onCheckedChanged(RadioContainer radioContainer, int i) {
                                RadioButton rb = (RadioButton) radioContainer.getComponentAt(i);
                                Text text1 = (Text) findComponentById(ResourceTable.Id_text_question_1);
                                Text text2 = (Text) findComponentById(ResourceTable.Id_text_question_2);
                                Text text3 = (Text) findComponentById(ResourceTable.Id_text_question_3);
                                if (rb.isChecked()) {
                                    if (i == 0) {
                                        text1.setText("        随着科学技术的迅速发展，在当前时代应用最为广泛的两大手机操作系统为谷歌Android和苹果iOS。而随着对国产高端技术的日益需求和国际竞争的愈加激烈，拥有自主知识产权的呼声更加高涨。鸿蒙操作系统（HarmonyOS）的问世，一定程度上满足当下的需求。HarmonyOS是一款面向万物互联时代的、全新的分布式操作系统，在传统的单设备系统能力基础上，可以基于同一套系统能力适配多种终端，能够支持手机、平板、智能穿戴、智慧屏、车机等多种终端设备，提供全场景（移动办公、运动健康、社交通信、媒体娱乐等）业务能力。HarmonyOS拥有分布架构、内核安全、生态共享、运行流畅四大优势。而游戏作为现生活不可或缺的一部分，社交、娱乐以及其带来的经济价值使之地位越来越重，俄罗斯方块作为经典游戏之一，也给许多人留下了深刻的印象。");
                                        text2.setText("        本俄罗斯方块基于HarmonyOS，以App的形式开发。拥有3种游戏模式，各具特色，但都秉承俄罗斯方块经典玩法规则，比如方块随机产生并提示玩家下一块即将下落方块的形态，方块下落时可使其旋转、加速以提高操作上限，方块填满一行时自动消除行。同时本游戏还保留了传统的积分模式和计时功能，驱使玩家在更短的时间内取得更高的分数而投入其中，而且玩家的游戏记录将会得到保存以便查看。");
                                        text3.setText("        采用Java语言开发，UI设计基于鸿蒙OS Java UI框架，每个用户界面的UI元素由 Component 和 ComponentContainer 对象构成，以组件、布局的形式将界面绘制在窗口上使得应用开发更加灵活。Page之间的导航采用显示使用Intent的方法。数据库采用目前使用最广泛的SQLite数据库。");
                                        bt_language.setText("language");
                                    } else {
                                        text1.setText("        With the rapid development of science and technology, the two most widely used mobile operating systems in the current era are Google Android and Apple iOS. With the increasing demand for domestic high-end technology and the intensification of international competition, the call for independent intellectual property rights has become even louder. The advent of HarmonyOS meets the needs of the moment to a certain extent. HarmonyOS is a new distributed operating system for the era of internet of everything, on the basis of the traditional single-device system capabilities, it can adapt to a variety of terminals based on the same set of system capabilities, and can support mobile phones, tablets, smart wearables, smart screens, car machines and other terminal devices, providing all-scenario (mobile office, sports health, social communication, media entertainment, etc.) business capabilities. HarmonyOS has four major advantages: distributed architecture, kernel security, ecological sharing, and smooth operation. As an indispensable part of life, the game has become more and more important to socializing, entertainment and the economic value it brings, and Tetris, as one of the classic games, has also left a deep impression on many people.");
                                        text2.setText("        Tetris for this article is based on HarmonyOS and is developed as an app. There are 3 game modes, each with its own characteristics, but all adhering to the classic gameplay rules of Tetris, such as the random generation of blocks and prompting the player of the form of the next block that is about to fall, the block can be rotated and accelerated to increase the upper limit of operation when the block falls, and the row is automatically eliminated when the block fills a row. At the same time, the game also retains the traditional points mode and timing function, driving players to achieve higher scores in a shorter period of time and invest in it, and the player's game record will be saved for viewing.");
                                        text3.setText("        Using Java language development, UI design based on HarmonyOS Java UI framework, the UI elements of each user interface by the Complex and ComponentContainer objects, in the form of components, layout of the interface drawn on the window to make the application development more flexible. Navigation between Pages takes the method of displaying using Intent.The database uses the most widely used SQLite database available today.");
                                        bt_language.setText("语言");

                                    }
                                    commondialog.destroy();
                                }
                            }
                        });

                    }
                });
            }
            //支持我
            Button bt_support = (Button) findComponentById(ResourceTable.Id_support);
            if (bt_support != null) {
                bt_support.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        DirectionalLayout dl = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                                .parse(ResourceTable.Layout_support, null, false);
                        MyCommondialog commondialog = new MyCommondialog(getContext(), dl);
                    }
                });

            }


        }

    }

    @Override
    public void onUnselected(TabList.Tab tab) {

    }

    @Override
    public void onReselected(TabList.Tab tab) {

    }
}
